package com.quanxiaoha.weblog.common.exception;

import com.quanxiaoha.weblog.common.utils.Response;
import com.quanxiaoha.weblog.common.enums.ResponseCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler({BizException.class})
    @ResponseBody
    public Response<Object> handleBizException(HttpServletRequest request, BizException e){
        log.warn("{} request fail,errorCode:{},errorMessage:{}",request.getRequestURI(),e.getErrorCode(),e.getErrorMessage());
        return Response.fail(e);
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public Response<Object> HandleOtherException(HttpServletRequest request,Exception e){
        log.error("{} request error,",request.getRequestURI(),e);
        return Response.fail(ResponseCodeEnum.SYSTEM_ERROR);
    }
    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseBody
    public Response<Object> handleMethodArgumentNotValidException(HttpServletRequest request,MethodArgumentNotValidException e){
        String errorCode=ResponseCodeEnum.PARAM_NOT_VALID.getErrorCode();

        BindingResult bindingResult=e.getBindingResult();

        StringBuilder sb=new StringBuilder();

        Optional.ofNullable(bindingResult.getFieldErrors()).ifPresent(errors->{
            errors.forEach(error->
                    sb.append(error.getField())
                            .append(" ")
                            .append(error.getDefaultMessage())
                            .append(", 当前值： ‘")
                            .append(error.getRejectedValue())
                            .append("';")
                    );
        });
        String errorMessage=sb.toString();
        log.warn("{} request error,errorcode:{}, errorMessage:{}",request.getRequestURI(),errorCode,errorMessage);

        return Response.fail(errorCode,errorMessage);
    }
}
